home *** CD-ROM | disk | FTP | other *** search
/ Champak 106 / Vol 106.iso / games / rain_for.swf / scripts / __Packages / BuildingHandler.as < prev    next >
Encoding:
Text File  |  2010-04-12  |  13.7 KB  |  427 lines

  1. class BuildingHandler
  2. {
  3.    var location;
  4.    var oHandler;
  5.    var aPower;
  6.    var aBuild;
  7.    var oWorld;
  8.    var nPowerMod;
  9.    var cPowerChance;
  10.    var tileBottomPlacement;
  11.    var tileMidPlacement;
  12.    var tileTopPlacement;
  13.    var nArrayLimit;
  14.    var oGerms;
  15.    static var tileWidth;
  16.    static var home;
  17.    static var tileWidthBot;
  18.    static var leftEdge;
  19.    static var topEdge;
  20.    static var tileHeightBot;
  21.    static var tileHeightMid;
  22.    static var tileHeightTop;
  23.    static var floor;
  24.    static var ceiling;
  25.    static var homeLimit;
  26.    static var tileWidthTop;
  27.    static var tileWidthMid;
  28.    var bAdd = false;
  29.    var nGermChance = 5;
  30.    var nTypes = 3;
  31.    var nTotalGerms = 0;
  32.    var critterFreq = 5;
  33.    var maxCritters = 5;
  34.    var cCritters = 0;
  35.    var swingChance = 2;
  36.    var bCheckPossible = false;
  37.    var nPowerChance = 3;
  38.    function BuildingHandler(loc)
  39.    {
  40.       this.location = loc;
  41.       this.oHandler = new smashing.ClipHandler(this.location);
  42.       this.aPower = ["BonusHang","BonusTime","BonusSpeed","BonusCoin"];
  43.    }
  44.    function update(nElapsed)
  45.    {
  46.       var _loc3_ = this.oHandler.clips;
  47.       var _loc4_ = _loc3_.length;
  48.       var _loc2_ = 0;
  49.       while(_loc2_ < _loc4_)
  50.       {
  51.          _loc3_[_loc2_].render();
  52.          _loc2_ = _loc2_ + 1;
  53.       }
  54.       if(this.bAdd == true)
  55.       {
  56.          this.addBuilding();
  57.       }
  58.    }
  59.    function render()
  60.    {
  61.       var _loc8_ = smashing.Viewport.x + smashing.Viewport.halfWidth;
  62.       var _loc9_ = _loc8_ - smashing.Viewport.width;
  63.       var _loc5_ = Math.floor(_loc8_ / BuildingHandler.tileWidth) + 1;
  64.       var _loc7_ = Math.floor(_loc9_ / BuildingHandler.tileWidth);
  65.       if(_loc5_ > this.aBuild.length)
  66.       {
  67.          this.addBuilding(this.oWorld.level);
  68.       }
  69.       var _loc3_ = undefined;
  70.       var _loc2_ = _loc7_;
  71.       while(_loc2_ < _loc5_)
  72.       {
  73.          _loc3_ = this.aBuild[_loc2_];
  74.          if(_loc3_.bOnStage == false)
  75.          {
  76.             this.buildOnStage(_loc3_);
  77.          }
  78.          _loc2_ = _loc2_ + 1;
  79.       }
  80.       var _loc4_ = this.oHandler.clips;
  81.       var _loc6_ = _loc4_.length;
  82.       _loc2_ = 0;
  83.       while(_loc2_ < _loc6_)
  84.       {
  85.          _loc4_[_loc2_].render();
  86.          _loc2_ = _loc2_ + 1;
  87.       }
  88.    }
  89.    function reset(nLevel)
  90.    {
  91.       this.nPowerMod = this.nPowerChance + nLevel;
  92.       this.cPowerChance = this.nPowerMod;
  93.       this.cCritters = 0;
  94.       this.oHandler.removeAllClips();
  95.       BuildingHandler.home = null;
  96.       this.aBuild = [];
  97.       this.init();
  98.    }
  99.    function init()
  100.    {
  101.       BuildingHandler.tileWidth = BuildingHandler.tileWidthBot;
  102.       BuildingHandler.leftEdge = - (smashing.Viewport.halfWidth + BuildingHandler.tileWidth);
  103.       BuildingHandler.topEdge = - smashing.Viewport.halfHeight;
  104.       this.tileBottomPlacement = smashing.Viewport.height - BuildingHandler.tileHeightBot;
  105.       this.tileMidPlacement = this.tileBottomPlacement - BuildingHandler.tileHeightMid;
  106.       this.tileTopPlacement = this.tileMidPlacement - BuildingHandler.tileHeightTop;
  107.       BuildingHandler.floor = this.tileBottomPlacement + BuildingHandler.tileHeightBot + 50;
  108.       BuildingHandler.ceiling = smashing.Viewport.height - BuildingHandler.tileHeightBot - BuildingHandler.tileHeightMid - BuildingHandler.tileHeightTop;
  109.       this.addBuilding(this.oWorld.level,false);
  110.       this.addBuilding(this.oWorld.level,false);
  111.       this.addBuilding(this.oWorld.level);
  112.       this.addBuilding(this.oWorld.level);
  113.       this.render();
  114.       this.nArrayLimit = Math.ceil(this.oWorld.nDist / BuildingHandler.tileWidth);
  115.    }
  116.    function removeBuilding(o, remove)
  117.    {
  118.       if(o.mcGerm != null)
  119.       {
  120.          _global.mcRoot.mcMenu.setUnFlash(o.mcGerm.sCrit);
  121.          o.mcGerm.handler.removeClip(o.mcGerm);
  122.       }
  123.       o.oData.bOnStage = false;
  124.       this.oHandler.removeClip(o);
  125.    }
  126.    function addBuilding(nLevel, bObst)
  127.    {
  128.       if(BuildingHandler.home != null)
  129.       {
  130.          return undefined;
  131.       }
  132.       var _loc2_ = new BData();
  133.       _loc2_.nType = Math.ceil(Math.random() * this.nTypes);
  134.       _loc2_.nLevel = (nLevel - 1) % 3;
  135.       if(bObst == undefined)
  136.       {
  137.          bObst = smashing.Misc.rndBool();
  138.          if(bObst == false)
  139.          {
  140.             _loc2_.bObstBot = false;
  141.             _loc2_.sPowerupBot = this.shouldAddPowerup();
  142.          }
  143.          else
  144.          {
  145.             _loc2_.bObstBot = true;
  146.          }
  147.       }
  148.       else
  149.       {
  150.          _loc2_.sPowerupBot = null;
  151.          _loc2_.bObstBot = false;
  152.       }
  153.       if(smashing.Misc.rndBool())
  154.       {
  155.          _loc2_.bObstMid = true;
  156.       }
  157.       else
  158.       {
  159.          _loc2_.bObstMid = false;
  160.          _loc2_.sPowerupMid = this.shouldAddPowerup();
  161.       }
  162.       if(smashing.Misc.rndBool())
  163.       {
  164.          _loc2_.bObstTop = true;
  165.       }
  166.       else
  167.       {
  168.          _loc2_.bObstTop = false;
  169.          _loc2_.sPowerupTop = this.shouldAddPowerup();
  170.       }
  171.       _loc2_.x = this.aBuild.length * BuildingHandler.tileWidth;
  172.       var _loc5_ = this.aBuild.length;
  173.       var _loc3_ = undefined;
  174.       if(_loc5_ > 0 && _loc5_ % this.critterFreq == 0 && this.cCritters < this.maxCritters)
  175.       {
  176.          this.cCritters = this.cCritters + 1;
  177.          var _loc8_ = Math.floor(_loc5_ / this.critterFreq);
  178.          var _loc10_ = this.oWorld.aPlacement[_loc8_ - 1];
  179.          var _loc6_ = this.oWorld.aCritterList[_loc8_ - 1];
  180.          if(_loc6_ != "sloth")
  181.          {
  182.             switch(_loc10_)
  183.             {
  184.                case 0:
  185.                   _loc2_.sCritBot = _loc6_;
  186.                   _loc2_.bObstBot = true;
  187.                   _loc2_.sPowerupBot = null;
  188.                   break;
  189.                case 1:
  190.                   _loc2_.sCritMid = _loc6_;
  191.                   _loc2_.bObstMid = true;
  192.                   _loc2_.sPowerupMid = null;
  193.                   break;
  194.                case 2:
  195.                   _loc2_.sCritTop = _loc6_;
  196.                   _loc2_.bObstTop = true;
  197.                   _loc2_.sPowerupTop = null;
  198.             }
  199.          }
  200.          else
  201.          {
  202.             _loc2_.sCritTop = _loc6_;
  203.             _loc2_.bObstTop = true;
  204.             _loc2_.sPowerupTop = null;
  205.          }
  206.       }
  207.       else if(_loc5_ > 4)
  208.       {
  209.          _loc3_ = this.aBuild[_loc5_ - 1];
  210.          if(_loc3_.bObstMid && !_loc3_.bObstTop)
  211.          {
  212.             if(0 == Math.floor(Math.random() * this.swingChance))
  213.             {
  214.                _loc2_.sPowerupBot = null;
  215.                _loc2_.bObstBot = false;
  216.                _loc2_.sPowerupMid = null;
  217.                _loc2_.bObstMid = false;
  218.                _loc2_.sPowerupTop = null;
  219.                _loc2_.bObstTop = false;
  220.                _loc2_.bSwing = true;
  221.             }
  222.          }
  223.       }
  224.       _loc3_ = this.aBuild[_loc5_ - 1];
  225.       if(this.aBuild.length > 2)
  226.       {
  227.          if(_loc2_.bSwing == false)
  228.          {
  229.             var _loc7_ = _loc2_.bObstBot + (_loc2_.bObstMid << 1) + (_loc2_.bObstTop << 2);
  230.             if(_loc3_.bSwing == false)
  231.             {
  232.                var _loc4_ = _loc3_.bObstBot + (_loc3_.bObstMid << 1) + (_loc3_.bObstTop << 2);
  233.                switch(_loc7_)
  234.                {
  235.                   case 0:
  236.                   case 3:
  237.                   case 7:
  238.                   case 1:
  239.                      break;
  240.                   case 2:
  241.                      if(_loc4_ == 0)
  242.                      {
  243.                         _loc2_.bObstBot = true;
  244.                         _loc2_.sPowerupBot = null;
  245.                      }
  246.                      break;
  247.                   case 4:
  248.                      if(_loc4_ == 0)
  249.                      {
  250.                         _loc2_.bObstMid = true;
  251.                         _loc2_.sPowerupMid = null;
  252.                         _loc2_.bObstBot = true;
  253.                         _loc2_.sPowerupBot = null;
  254.                      }
  255.                      else if(_loc4_ == 1)
  256.                      {
  257.                         _loc2_.bObstMid = true;
  258.                         _loc2_.sPowerupMid = null;
  259.                      }
  260.                      break;
  261.                   case 5:
  262.                      if(_loc4_ == 0)
  263.                      {
  264.                         _loc2_.bObstMid = true;
  265.                         _loc2_.sPowerupMid = null;
  266.                      }
  267.                      else if(_loc4_ == 1)
  268.                      {
  269.                         _loc2_.bObstMid = true;
  270.                         _loc2_.sPowerupMid = null;
  271.                      }
  272.                      break;
  273.                   case 6:
  274.                      if(_loc4_ == 0)
  275.                      {
  276.                         _loc2_.bObstBot = true;
  277.                         _loc2_.sPowerupBot = null;
  278.                      }
  279.                }
  280.             }
  281.             else
  282.             {
  283.                switch(_loc7_)
  284.                {
  285.                   case 5:
  286.                   case 4:
  287.                   case 0:
  288.                   case 1:
  289.                      _loc2_.bObstMid = true;
  290.                      _loc2_.sPowerupMid = null;
  291.                }
  292.             }
  293.          }
  294.       }
  295.       else if(this.aBuild.length == 2)
  296.       {
  297.          if(_loc2_.bObstBot == false)
  298.          {
  299.             if(_loc2_.bObstMid || _loc2_.bObstTop)
  300.             {
  301.                if(_loc2_.bObstTop)
  302.                {
  303.                   _loc2_.bObstMid = true;
  304.                   _loc2_.sPowerupMid = null;
  305.                }
  306.                if(_loc2_.bObstMid)
  307.                {
  308.                   _loc2_.bObstBot = true;
  309.                   _loc2_.sPowerupBot = null;
  310.                }
  311.             }
  312.          }
  313.       }
  314.       this.aBuild.push(_loc2_);
  315.    }
  316.    function buildOnStage(oBuild)
  317.    {
  318.       oBuild.bOnStage = true;
  319.       if(oBuild.bHome == false)
  320.       {
  321.          var _loc4_ = undefined;
  322.          var _loc3_ = undefined;
  323.          var _loc5_ = this.oHandler.addClip("mcTreeBottom0");
  324.          _loc5_.x = oBuild.x;
  325.          _loc5_.y = this.tileBottomPlacement;
  326.          _loc5_.init(this,oBuild.bObstBot,oBuild.nType,oBuild,0);
  327.          if(oBuild.sPowerupBot != null)
  328.          {
  329.             _loc4_ = this.oGerms.addGerm(_loc5_.x + _loc5_.mcBonusLoc._x,_loc5_.y + _loc5_.mcBonusLoc._y,oBuild.sPowerupBot);
  330.             _loc4_.oData = oBuild;
  331.             _loc4_.nSegment = 0;
  332.             _loc4_.mcBuild = _loc5_;
  333.             _loc5_.mcGerm = _loc4_;
  334.          }
  335.          else if(oBuild.sCritBot != undefined)
  336.          {
  337.             _loc3_ = this.oGerms.addGerm(_loc5_.x + _loc5_.gfx.mcGermPlacer._x,_loc5_.y + _loc5_.gfx.mcGermPlacer._y,"mcGerm");
  338.             _loc3_.oData = oBuild;
  339.             _loc3_.nSegment = 0;
  340.             _loc3_.mcBuild = _loc5_;
  341.             _loc3_.setCritter(oBuild.sCritBot);
  342.             _loc3_.sCrit = oBuild.sCritBot;
  343.             _loc5_.mcGerm = _loc3_;
  344.          }
  345.          _loc5_ = this.oHandler.addClip("mcTreeMiddle0");
  346.          _loc5_.x = oBuild.x;
  347.          _loc5_.y = this.tileMidPlacement;
  348.          _loc5_.init(this,oBuild.bObstMid,oBuild.nType,oBuild,0);
  349.          if(oBuild.sPowerupMid != null)
  350.          {
  351.             _loc4_ = this.oGerms.addGerm(_loc5_.x + _loc5_.mcBonusLoc._x,_loc5_.y + _loc5_.mcBonusLoc._y,oBuild.sPowerupMid);
  352.             _loc4_.oData = oBuild;
  353.             _loc4_.nSegment = 1;
  354.             _loc4_.mcBuild = _loc5_;
  355.             _loc5_.mcGerm = _loc4_;
  356.          }
  357.          else if(oBuild.sCritMid != undefined)
  358.          {
  359.             _loc3_ = this.oGerms.addGerm(_loc5_.x + _loc5_.gfx.mcGermPlacer._x,_loc5_.y + _loc5_.gfx.mcGermPlacer._y,"mcGerm");
  360.             _loc3_.oData = oBuild;
  361.             _loc3_.nSegment = 1;
  362.             _loc3_.mcBuild = _loc5_;
  363.             _loc3_.setCritter(oBuild.sCritMid);
  364.             _loc3_.sCrit = oBuild.sCritMid;
  365.             _loc5_.mcGerm = _loc3_;
  366.          }
  367.          _loc5_ = this.oHandler.addClip("mcTreeTop0");
  368.          _loc5_.x = oBuild.x;
  369.          _loc5_.y = this.tileTopPlacement;
  370.          _loc5_.init(this,oBuild.bObstTop,oBuild.nType,oBuild,0);
  371.          if(oBuild.sPowerupTop != null)
  372.          {
  373.             _loc4_ = this.oGerms.addGerm(_loc5_.x + _loc5_.mcBonusLoc._x,_loc5_.y + _loc5_.mcBonusLoc._y,oBuild.sPowerupTop);
  374.             _loc4_.oData = oBuild;
  375.             _loc4_.nSegment = 2;
  376.             _loc4_.mcBuild = _loc5_;
  377.             _loc5_.mcGerm = _loc4_;
  378.          }
  379.          else if(oBuild.sCritTop != undefined)
  380.          {
  381.             _loc3_ = this.oGerms.addGerm(_loc5_.x + _loc5_.gfx.mcGermPlacer._x,_loc5_.y + _loc5_.gfx.mcGermPlacer._y,"mcGerm");
  382.             _loc3_.oData = oBuild;
  383.             _loc3_.nSegment = 2;
  384.             _loc3_.mcBuild = _loc5_;
  385.             _loc3_.setCritter(oBuild.sCritTop);
  386.             _loc3_.sCrit = oBuild.sCritTop;
  387.             _loc5_.mcGerm = _loc3_;
  388.          }
  389.          if(oBuild.bSwing)
  390.          {
  391.             _loc4_ = this.oGerms.addGerm(_loc5_.x + _loc5_.mcBonusLoc._x,_loc5_.y + _loc5_.mcBonusLoc._y,"mcSwing");
  392.             _loc5_.mcGerm = _loc4_;
  393.          }
  394.       }
  395.       else
  396.       {
  397.          _loc5_ = this.oHandler.addClip("home");
  398.          _loc5_.x = oBuild.x;
  399.          _loc5_.y = BuildingHandler.floor - 50;
  400.          BuildingHandler.homeLimit = _loc5_.x + BuildingHandler.tileWidth - smashing.Viewport.halfWidth;
  401.          BuildingHandler.home = _loc5_;
  402.       }
  403.    }
  404.    function addHome()
  405.    {
  406.       var _loc2_ = new BData();
  407.       _loc2_.bHome = true;
  408.       _loc2_.x = this.aBuild.length * BuildingHandler.tileWidth;
  409.       this.aBuild.push(_loc2_);
  410.    }
  411.    function shouldAddGerm()
  412.    {
  413.       return 0 == Math.floor(Math.random() * this.nGermChance);
  414.    }
  415.    function shouldAddPowerup()
  416.    {
  417.       if(0 == Math.floor(Math.random() * this.cPowerChance))
  418.       {
  419.          this.cPowerChance = this.nPowerMod;
  420.          var _loc2_ = this.aPower[Math.floor(Math.random() * this.aPower.length)];
  421.          return _loc2_;
  422.       }
  423.       this.cPowerChance = this.cPowerChance - 1;
  424.       return null;
  425.    }
  426. }
  427.